package com.dykj.fireFighting.server.fire.devhis.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dykj.fireFighting.api.fire.devhis.dto.DevHisDto;
import com.dykj.fireFighting.api.fire.devhis.dto.DeviceDto;
import com.dykj.fireFighting.api.fire.devhis.entity.Device;
import com.dykj.fireFighting.api.fire.devhis.entity.vo.DevHisVo;
import com.dykj.fireFighting.api.fire.devhis.entity.vo.DevHisWaterVo;
import com.dykj.fireFighting.api.fire.devhis.service.IDevHisService;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.common.starter.redis.service.RedisService;
import com.dykj.fireFighting.server.fire.devhis.mapper.DevHisMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author fgc
 * @Date 2021/8/31 13:32
 * @ Description：运行记录服务方法实现类
 */
@DubboService
public class DevHisServiceImpl implements IDevHisService {
    @Autowired
    private DevHisMapper devHisMapper;
    @Autowired
    private RedisService redisService;
    @Override
    public PageVo<DevHisVo> getDevHisList(DevHisDto devHisDto) {
        Page page=new Page();
        // 设置当前页码
        page.setCurrent(devHisDto.getPage());
        // 设置页大小
        page.setSize(devHisDto.getLimit());
        return new PageVo<>(devHisMapper.getDevHisList(page,devHisDto));
    }

    @Override
    public PageVo<DevHisVo> getDevHisFaultList(DevHisDto devHisDto) {
        Page page=new Page();
        // 设置当前页码
        page.setCurrent(devHisDto.getPage());
        // 设置页大小
        page.setSize(devHisDto.getLimit());
        IPage<DevHisVo> devHisFaultList = devHisMapper.getDevHisFaultList(page, devHisDto);
        if(devHisDto.getWbRead().equals("0")){
            devHisMapper.updateFaultReadStatus(devHisDto);
        }
        return new PageVo<>(devHisFaultList);
    }

    @Override
    public int getFaultNotRead(DevHisDto devHisDto) {
        return devHisMapper.getFaultNotRead(devHisDto);
    }

    @Override
    public PageVo<Device> pageList(DeviceDto deviceDto) {
        Page page=new Page();
        page.setCurrent(deviceDto.getPage());
        page.setSize(deviceDto.getLimit());
        IPage<Device> deviceIPage = devHisMapper.pageList(page, deviceDto);
        for (Device d:deviceIPage.getRecords()) {
            //获取设备当前状态
            String bjzt = (String)redisService.get(d.getRelationId());
            if(StringUtils.isNotBlank(bjzt)){//判断缓存中是否存在
                JSONObject o = JSON.parseObject(bjzt);
                Object oo=o.get("status");
                if(null!=oo){
                    d.setStatus(oo.toString());
                }else{
                    if("728181".equals(d.getDevSysType())){//火灾自动报警设备
                        String fireDevStatus = devHisMapper.getFireDevStatus(d.getRelationId());
                        d.setStatus(fireDevStatus);
                    }else if("728183".equals(d.getDevSysType())){
                        d.setStatus(devHisMapper.getWaterDevStatus(d.getRelationId()));
                    }else if("728184".equals(d.getDevSysType())){
                        String electricalDevStatus = devHisMapper.getElectricalDevStatus(d.getRelationId());
                        d.setStatus(electricalDevStatus);
                    }
                }
            }else{
                //缓存中不存在
                if("728181".equals(d.getDevSysType())){//火灾自动报警设备
                    String fireDevStatus = devHisMapper.getFireDevStatus(d.getRelationId());
                    d.setStatus(fireDevStatus);
                }else if("728183".equals(d.getDevSysType())){
                    d.setStatus(devHisMapper.getWaterDevStatus(d.getRelationId()));
                }else if("728184".equals(d.getDevSysType())){
                    String electricalDevStatus = devHisMapper.getElectricalDevStatus(d.getRelationId());
                    d.setStatus(electricalDevStatus);
                }
            }
        }
        return new PageVo<>(deviceIPage);
    }

    @Override
    public PageVo<DevHisWaterVo> getDevHisWater(long pageNum, long limit, String relationId) {
        Page page=new Page();
        page.setCurrent(pageNum);
        page.setSize(limit);
        return new PageVo<>(devHisMapper.getDevHisWater(page,relationId));
    }
}
